package org.osami.gateway.actuator.internal.swing;

import java.util.logging.Logger;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;

import org.osami.gateway.actuator.Actuator;
import org.osami.gateway.watteco.interfaces.ISwitch;

/**
 * Created by IntelliJ IDEA.
 * User: guillaume
 * Date: 09/12/10
 * Time: 18:08
 * To change this template use File | Settings | File Templates.
 */
public class ActuatorFrame extends JFrame {
    private static final Logger logger = Logger.getLogger(ActuatorFrame.class.getName());

    private JLabel status;
    private JLabel imageLabel;
    private JLabel title;
    private JLabel text;
    private ImageIcon blueIcon;
    private ImageIcon greenIcon;
    private ImageIcon redIcon;
    private ImageIcon imageIcon;

    public ActuatorFrame(String actuatorType, final ISwitch model) throws HeadlessException {
        super();

        JPanel container = new JPanel();
        add(container);

        // Title
        title = new JLabel();
        container.add(title);

        // Panel State
        JPanel statusPanel = new JPanel();
        container.add(statusPanel);

        URL blue = getClass().getResource("blue.gif");
        blueIcon = new ImageIcon(blue);
        URL green = getClass().getResource("green.gif");
        greenIcon = new ImageIcon(green);
        URL red = getClass().getResource("red.gif");
        redIcon = new ImageIcon(red);

        URL image = null;
        imageLabel = new JLabel();
        if (!"".equals(actuatorType)) {
            if (actuatorType.equals(Actuator.TYPE_LIGHT)) {
                image = getClass().getResource("light.gif");
            }
            else if (actuatorType.equals(Actuator.TYPE_TV)) {
                image = getClass().getResource("tv.gif");
            }
            else if (actuatorType.equals(Actuator.TYPE_WINDOW)) {
                image = getClass().getResource("window.gif");
            }
            else {
                logger.severe("Unknown actuator type \"" + actuatorType + "\"");
            }
        }

        if (image != null) {
            imageIcon = new ImageIcon(image);
            imageLabel.setIcon(imageIcon);
            statusPanel.add(imageLabel);
        }

        status = new JLabel();
        status.setIcon(blueIcon);
        statusPanel.add(status);

        JButton button = new JButton();
        container.add(button);
        button.setText("Change state");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                try {
                    model.toggleSwitch();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        // Additional text
        text = new JLabel();
        container.add(text);
    }

    public void switchOn() {
        status.setIcon(greenIcon);
    }

    public void switchOff() {
        status.setIcon(redIcon);
    }

    public void setSerial(String serial) {
        title.setText(serial);
    }

    public void setText(String newText) {
        text.setText(newText);
    }
}
